// <copyright file="Paginator.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using YUIAspNet.Common;

#region Embedded resources definitions
[assembly: WebResource("YUIAspNet.Paginator.Paginator.js", "text/javascript")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Paginator.paginator-skin.css", "text/css", PerformSubstitution = true)]
#endregion

namespace YUIAspNet
{
    /// <summary>
    /// Paginator control, used for displaying long content on more than one page.
    /// </summary>
    [ToolboxItem(true)]
    [ParseChildren(true)]
    public class Paginator : YUIScriptBaseControl, INamingContainer
    {
        private IList<string> _containers;
        private Dictionary<int, string> _rowPerPageOptions;

        /// <summary>
        /// Registers the style sheets for the current control.
        /// Must be overriden in the parent control to add any css file reference.
        /// </summary>
        protected override void RegisterStyleSheets()
        {
            RegisterEmbeddedStylesheet("Paginator", "paginator-skin.css");
        }

        /// <summary>
        /// Register the values to send to the client control for initialization.
        /// </summary>
        protected override void RegisterClientInitialization()
        {
            AddProperty("RowsPerPage", RowsPerPage);
            AddProperty("TotalRecords", TotalRecords);
            AddProperty("Containers", Containers);
            
            AddProperty("InitialPage", InitialPage);
            AddProperty("AlwaysVisible", AlwaysVisible);
            AddProperty("RecordOffset", RecordOffset);
            AddProperty("UpdateOnChange", UpdateOnChange);
            AddProperty("PageLinks", PageLinks);

            if (!string.IsNullOrEmpty(FirstPageLinkText))
                AddProperty("FirstPageLinkText", FirstPageLinkText);
            if (!string.IsNullOrEmpty(PreviousPageLinkText))
                AddProperty("PreviousPageLinkText", PreviousPageLinkText);
            if (!string.IsNullOrEmpty(NextPageLinkText))
                AddProperty("NextPageLinkText", NextPageLinkText);
            if (!string.IsNullOrEmpty(LastPageLinkText))
                AddProperty("LastPageLinkText", LastPageLinkText);
            if (!string.IsNullOrEmpty(PageReportTemplate))
                AddProperty("PageReportTemplate", PageReportTemplate);
            if (!string.IsNullOrEmpty(Template))
                AddProperty("Template", Template);

            if(!string.IsNullOrEmpty(OnClientPageChange))
                AddEvent("OnPageChange", OnClientPageChange);

        }

        /// <summary>
        /// Registers any additional scripts to include for this control.
        /// </summary>
        protected override void RegisterScriptReferences()
        {
            AddScriptReference(new ScriptReference("YUIAspNet.Paginator.Paginator.js", this.GetType().Assembly.FullName));
        }

        #region Public properties
        /// <summary>
        /// Array of available rows-per-page sizes.  Converted into select options.
        /// </summary>
        /// <value>The rows per page options.</value>
        public IDictionary<int, string> RowsPerPageOptions
        {
            get
            {
                if (_rowPerPageOptions == null)
                    _rowPerPageOptions = new Dictionary<int, string>();
                return _rowPerPageOptions;
            }
        }

        /// <summary>
        /// Gets or sets the template.
        /// </summary>
        /// <value>The template.</value>
        public string Template { get; set; }

        /// <summary>
        /// Gets or sets the page report template.
        /// </summary>
        /// <value>The page report template.</value>
        public string PageReportTemplate { get; set; }

        /// <summary>
        /// Gets or sets the page links.
        /// </summary>
        /// <value>The page links.</value>
        public int PageLinks { get; set; }

        /// <summary>
        /// Gets or sets the rows per page.
        /// </summary>
        /// <value>The rows per page.</value>
        public int RowsPerPage { get; set; }

        /// <summary>
        /// Gets or sets the total records.
        /// </summary>
        /// <value>The total records.</value>
        public int TotalRecords { get; set; }

        /// <summary>
        /// Gets or sets the containers.
        /// </summary>
        /// <value>The containers.</value>
        public IList<string> Containers 
        { 
            get
            {
                if(_containers == null)
                    _containers = new List<string>();
                return _containers;
            }
        }

        /// <summary>
        /// Gets or sets the initial page.
        /// </summary>
        /// <value>The initial page.</value>
        public int InitialPage { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the paginator is always visible.
        /// </summary>
        /// <value><c>true</c> if always visible; otherwise, <c>false</c>.</value>
        public bool AlwaysVisible { get; set; }

        /// <summary>
        /// Gets or sets the record offset.
        /// </summary>
        /// <value>The record offset.</value>
        public int RecordOffset { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether to update its state on change.
        /// </summary>
        /// <value><c>true</c> to update on change; otherwise, <c>false</c>.</value>
        public bool UpdateOnChange { get; set; }

        /// <summary>
        /// Gets or sets the first page link text.
        /// </summary>
        /// <value>The first page link text.</value>
        public string FirstPageLinkText { get; set; }

        /// <summary>
        /// Gets or sets the previous page link text.
        /// </summary>
        /// <value>The previous page link text.</value>
        public string PreviousPageLinkText { get; set; }

        /// <summary>
        /// Gets or sets the next page link text.
        /// </summary>
        /// <value>The next page link text.</value>
        public string NextPageLinkText { get; set; }

        /// <summary>
        /// Gets or sets the last page link text.
        /// </summary>
        /// <value>The last page link text.</value>
        public string LastPageLinkText { get; set; }

        /// <summary>
        /// Gets or sets the number of links.
        /// </summary>
        /// <value>The number of links.</value>
        public int NumberOfLinks { get; set; }

        /// <summary>
        /// Gets or sets the on client page change.
        /// </summary>
        /// <value>The on client page change.</value>
        public string OnClientPageChange { get; set; } 
        #endregion
    }
}
